package com.xujiang.myblog.util;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;

import com.miguelbcr.ui.rx_paparazzo.RxPaparazzo;
import com.miguelbcr.ui.rx_paparazzo.entities.size.OriginalSize;
import com.miguelbcr.ui.rx_paparazzo.entities.size.ScreenSize;
import com.miguelbcr.ui.rx_paparazzo.entities.size.SmallSize;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.xujiang.myblog.adapter.DlogAdapter;

import java.util.Arrays;
import java.util.List;
import com.miguelbcr.ui.rx_paparazzo.entities.size.Size;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by xujiang on 2017/4/26.
 */

public class TakeImagesHelper {
    private String[] items = {"选择本地照片", "拍照"};
    List<String> wordList = Arrays.asList(items);
    Callback2<String> mCallback2;
    DialogPlus dialogPlus;
    private Size size;

    private TakeImagesHelper(Context context){
        size = new OriginalSize();


    }

    public static TakeImagesHelper with(Activity activity) {
        return new TakeImagesHelper(activity.getApplicationContext()).createDialog(activity);
    }

    public static TakeImagesHelper with(Fragment fragment) {
        return new TakeImagesHelper(fragment.getContext()).createDialog(fragment);
    }

    private TakeImagesHelper createDialog(Activity activity){
        dialogPlus = DialogPlus.newDialog(activity).setAdapter(new DlogAdapter(activity,wordList)).setOnItemClickListener(
                new OnItemClickListener() {
                    @Override
                    public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                        if(position == 0){
                            RxPaparazzo.takeImage(activity).crop().usingGallery()
                                    .subscribe(response -> {
                                        if(response.resultCode() != Activity.RESULT_OK){
                                            return;
                                        }
                                        mCallback2.onSuccess(response.data());
                                    },throwable -> mCallback2.onError(throwable),
                                            ()->{
                                                dialog.dismiss();
                                                mCallback2.onFinish();
                                            });

                        }else if (position == 1){
                            RxPaparazzo.takeImage(activity).crop().usingCamera()
                                    .subscribe(response ->{
                                        if(response.resultCode() != Activity.RESULT_OK){
                                            return;
                                        }
                                        mCallback2.onSuccess(response.data());
                                    },throwable -> mCallback2.onError(throwable)
                                    ,()->{
                                                dialog.dismiss();
                                                mCallback2.onFinish();
                                            });
                        }
                    }
                }).create();
        return this;
    }

    private TakeImagesHelper createDialog(final Fragment fragment) {
        /***
         * SmallSize:大约1/8。屏幕的分辨率
         ScreenSize:大约大小的图像匹配。屏幕分辨率。
         OriginalSize:图像的原始大小。
         CustomMaxSize:Yot可以指定最大值大小和图像缩放比例。
         *
         * */
        size = new ScreenSize();
        dialogPlus = DialogPlus.newDialog(fragment.getContext())
                .setAdapter(new DlogAdapter(fragment.getContext(), wordList))
                .setOnItemClickListener(
                        new OnItemClickListener() {
                            @Override
                            public void onItemClick(final DialogPlus dialog, Object item, View view, int position) {

                                if (position == 0){
                                    RxPaparazzo.takeImage(fragment)
                                            .crop()
                                            .usingGallery()
                                            //.subscribeOn(Schedulers.io())
                                            //.observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(response->{
                                                if(response.resultCode() != Activity.RESULT_OK){
                                                    return;
                                                }

                                                mCallback2.onSuccess(response.data());
                                            },throwable -> mCallback2.onError(throwable),
                                                    ()->{
                                                        dialog.dismiss();
                                                        mCallback2.onFinish();
                                                    });

                                }else if (position == 1){
                                    RxPaparazzo.takeImage(fragment)
                                            .crop()
                                            .size(size)
                                            .usingCamera()
//                                            .subscribeOn(Schedulers.io())
//                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(response -> {
                                                if (response.resultCode() != Activity.RESULT_OK){
                                                    return;
                                                }
                                                mCallback2.onSuccess(response.data());
                                            }, throwable->mCallback2.onError(throwable),
                                                    ()->{
                                                        dialog.dismiss();
                                                        mCallback2.onFinish();
                                                    });


                                }

                            }
                        }).create();
        return this;
    }

    public void showTakeImageDialog(Callback2<String> callback) {
        mCallback2 = callback;
        dialogPlus.show();
    }



}
